package com.choosefine.it.manager.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.base.SendPush;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.manager.dao.ManagerFindprojectInfoMapper;
import com.choosefine.it.manager.model.ManagerFindprojectInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author 潘钱钦
	下午8:17:57
 *
 */
@Service("managerFindprojectInfoService")
public class ManagerFindprojectInfoService extends BaseService {
	@Autowired
	private ManagerFindprojectInfoMapper managerFindprojectInfoMapper;
	
	@Autowired
	private SendPush sendPush;
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Transactional
	public int addFindProject(ManagerFindprojectInfo managerFindprojectInfo) {
		//用户校验
		userInfoUserCodeCheck(managerFindprojectInfo.getUserCode());
		managerFindprojectInfo.setCtime(DateUtils.getNowTime());
		int num = managerFindprojectInfoMapper.insertSelective(managerFindprojectInfo); 
		if (num > 0){
			mongoTemplate.insert(managerFindprojectInfo);
		}
		return num;
	}
	
	@Transactional
	public int deleteManagerFindprojectInfoById(Map<String, Object> params) {
		int num = managerFindprojectInfoMapper.updateIsValidByIdFindproject(params); 
		if (num > 0){
			Update update = new Update();
			update.set("isValid", 1);
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, ManagerFindprojectInfo.class);
		}
		return num;
	}
	
	@Transactional
	public int updateManagerFindprojectInfoStateById(Map<String, Object> params) {
		int num = managerFindprojectInfoMapper.updateStateByIdFindproject(params); 
		if (num > 0){
			Update update = new Update();
			update.set("utime", params.get("utime"));
			update.set("publishStatus", params.get("publishStatus"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, ManagerFindprojectInfo.class);
		}
		return num;
	}
	
	public Page<ManagerFindprojectInfo> selectManagerFindprojectInfoListByUserCode(Map<String, Object> params,
			PageDTO pageDTO) {
		
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
	    Page<ManagerFindprojectInfo> page = managerFindprojectInfoMapper.selectManagerFindprojectInfoByUserCode(params);
		return page;
	}
	
	@Transactional
	public int updateManagerFindprojectInfo(ManagerFindprojectInfo m) {
		int num = managerFindprojectInfoMapper.updateByPrimaryKeySelective(m); 
		if (num > 0){
			Update update = mongoUpdateManagerFindProjectinfo(m);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(m.getId())), update, ManagerFindprojectInfo.class);
		}
		return num;
	}
	
	private Update mongoUpdateManagerFindProjectinfo(ManagerFindprojectInfo m) {
		Update update = new Update();
		if (!StringUtils.isEmpty(m.getProvinceCode())){
			update.set("provinceCode", m.getProvinceCode());
		}
		if (!StringUtils.isEmpty(m.getProvinceName())){
			update.set("provinceName", m.getProvinceName());
		}
		if (!StringUtils.isEmpty(m.getCityCode())){
			update.set("cityCode", m.getCityCode());
		}
		if (!StringUtils.isEmpty(m.getCityName())){
			update.set("cityName", m.getCityName());
		}
		if (!StringUtils.isEmpty(m.getDescribe())){
			update.set("describe", m.getDescribe());
		}
		if (null != m.getExpectProjectTime()){
			update.set("expectProjectTime", m.getExpectProjectTime());
		}
		if (!StringUtils.isEmpty(m.getExpectProjectTypeId())){
			update.set("expectProjectTypeId", m.getExpectProjectTypeId());
		}
		if (!StringUtils.isEmpty(m.getExpectProjectTypeName())){
			update.set("expectProjectTypeName", m.getExpectProjectTypeName());
		}
		if (!StringUtils.isEmpty(m.getPhone())){
			update.set("phone", m.getPhone());
		}
		if (!StringUtils.isEmpty(m.getProjectManagerName())){
			update.set("projectManagerName", m.getProjectManagerName());
		}
		if (!StringUtils.isEmpty(m.getSkillCertificateName())){
			update.set("skillCertificateName", m.getSkillCertificateName());
		}
		if (!StringUtils.isEmpty(m.getSparePhone())){
			update.set("sparePhone", m.getSparePhone());
		}
		update.set("utime", m.getUtime());
		update.set("releaseNo", m.getReleaseNo());
		update.set("reason", null);
		return update;
	}
	
	public Page<ManagerFindprojectInfo> selectFindProjectInfoByCondition(Map<String, Object> params, PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
	    Page<ManagerFindprojectInfo> page = managerFindprojectInfoMapper.selectFindProjectInfoByCondition(params);
	    for(int i = 0; i < page.size(); i++) {
	    	page.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(page.get(i).getId())), ManagerFindprojectInfo.class));
	    }
		return page;
	}
	
	@Transactional
	public int updateFindProjectVerifyStatus(Map<String, Object> params) {
		int num=0;
		num=managerFindprojectInfoMapper.updateFindProjectVerifyStatus(params);
		if(num>0){
					//对用户进行推送审核结果的通知
//					ArrayList<Integer> idss=(ArrayList<Integer>) params.get("ids");
//					Integer verifyStatus= (Integer) params.get("verifyStatus");
//					for(Integer id:idss){
//						ManagerFindprojectInfo managerFindprojectInfo = managerFindprojectInfoMapper.selectManagerFindprojectById(id);
//					if(managerFindprojectInfo!=null){
//						String userCode = managerFindprojectInfo.getUserCode();
//						Assert.assertNotNull(userCode, "用户编号为空");
//						if(verifyStatus==1){
//							sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISVERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISVERIFY);
//						}else{
//							sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISNOT_VERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISNOT_VERIFY);
//						}
//					}
//					}
			Update update = new Update();
			update.set("verifyStatus", params.get("verifyStatus"));
			update.set("publisher", params.get("publisher"));
			if (params.containsKey("reason")){
				update.set("reason", params.get("reason"));
			}
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, ManagerFindprojectInfo.class);
		}
		return num;
	}
	
	@Transactional
	public int deleteFindProject(Map<String, Object> params) {
		List<Integer> list = (List<Integer>)params.get("ids"); 
		int num = managerFindprojectInfoMapper.deleteFindProject(params); 
		if (num > 0){
			for(Integer id : list){
				Update update = new Update();
				update.set("utime", params.get("utime"));
				update.set("isValid", 1);
				mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)), update, ManagerFindprojectInfo.class);
			}
		}
		return num;
	}
	
	public ManagerFindprojectInfo getManagerFindprojectInfoById(Integer id) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), ManagerFindprojectInfo.class);
	}

}
